package cn.org.eshow.service.impl;

import cn.org.eshow.bean.query.CartQuery;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.dao.CartDao;
import cn.org.eshow.model.Cart;
import cn.org.eshow.model.Product;
import cn.org.eshow.model.ProductItem;
import cn.org.eshow.model.User;
import cn.org.eshow.service.CartManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.util.List;

@WebService(serviceName = "CartService", endpointInterface = "cn.org.eshow.service.CartManager")
@Service
public class CartManagerImpl extends GenericManagerImpl<Cart, Integer> implements CartManager {

    @Autowired
    CartDao cartDao;

    public CartManagerImpl() {
    }

    @Autowired
    public CartManagerImpl(CartDao cartDao) {
        super(cartDao);
        this.cartDao = cartDao;
    }

    @Override
    public List<Cart> list(CartQuery query) {
        return cartDao.list(query);
    }

    @Override
    public Page<Cart> search(CartQuery query) {
        return cartDao.search(query);
    }

    @Override
    public Cart check(CartQuery query) {
        List<Cart> list = cartDao.list(query);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public int count(CartQuery query) {
        List<Cart> list = cartDao.list(query);
        if (list.isEmpty()) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Cart save(ProductItem productItem,Integer productSize, User user) {
        Cart cart=check(new CartQuery(productItem.getId(),user.getId()));
        if(cart==null){
            cart = new Cart();
            cart.setQuantity(productSize);
            Product product=productItem.getProduct();
            cart.setType(product.getIfDiscount() != null && product.getIfDiscount().equals(Boolean.TRUE) ? "促销价" : "正常价");
            cart.setProduct(product);
            cart.setProductItem(productItem);
            cart.setUser(user);
            cart.setPrice(productItem.getPrice());
            return cartDao.save(cart);
        }else {
            cart.setQuantity(cart.getQuantity()+productSize);
            cart.setPrice(productItem.getPrice()*cart.getQuantity());
            return cartDao.save(cart);
        }

    }

    @Override
    public Cart update(Cart cart, Integer qunantity) {
        ProductItem productItem=cart.getProductItem();
        cart.setQuantity(qunantity);
        cart.setPrice(productItem.getPrice()*cart.getQuantity());
        return cartDao.save(cart);
    }

    @Override
    public void clear(User user) {
        CartQuery query = new CartQuery();
        query.setUserId(user.getId());
        List<Cart> list = cartDao.list(query);
        for (Cart cart : list) {
            this.remove(cart);
        }
    }


    @Override
    public void remove(List<Cart> carts) {
        for (Cart cart : carts) {
            cartDao.remove(cart);
        }
    }

}